//
//  CAShapeLayer+Extension.m
//  XuedaOnlineSchool
//
//  Created by 学大教育 on 2020/11/7.
//  Copyright © 2020 JackMac. All rights reserved.
//

#import "CAShapeLayer+Extension.h"

@implementation CAShapeLayer (Extension)
- (void)cornerRadius:(CGFloat)radius fillColor:(UIColor *)fillColor
borderWidth:(CGFloat)borderWidth
         borderColor:(UIColor *_Nullable)borderColor
{
    [self cornerRadius:radius fillColor:fillColor corners:UIRectCornerAllCorners borderWidth:borderWidth borderColor:borderColor borderLineJoin:kCGLineJoinRound];
}

- (void)cornerRadius:(CGFloat)radius fillColor:(UIColor *)fillColor corners:(UIRectCorner)corners
{
    [self cornerRadius:radius fillColor:fillColor corners:corners borderWidth:0 borderColor:nil borderLineJoin:kCGLineJoinRound];
}
- (void)cornerRadius:(CGFloat)radius fillColor:(UIColor *)fillColor
    corners:(UIRectCorner)corners
borderWidth:(CGFloat)borderWidth
borderColor:(UIColor *_Nullable)borderColor
      borderLineJoin:(CGLineJoin)borderLineJoin
{
    
    CGRect strokeRect = CGRectInset(self.bounds, borderWidth/2.0, borderWidth/2.0);
    
     UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:strokeRect byRoundingCorners:corners cornerRadii:CGSizeMake(radius , borderWidth)];
   
     self.path = path.CGPath;
     self.shouldRasterize = YES;
     self.rasterizationScale = [UIScreen mainScreen].scale;
     
    //设置边框及填充色
     
    self.fillColor = fillColor.CGColor;
     if (borderColor) {
         self.strokeColor = borderColor.CGColor;
     }
     
     if (borderWidth > 0) {
         self.lineWidth = borderWidth;
     }
     path.lineJoinStyle = borderLineJoin;
}

+ (CAShapeLayer *)drawTriangle:(CGRect)bounds fillColor:(UIColor *)fillColor upper:(BOOL)upper
{
    CAShapeLayer *triangleLayer = [[CAShapeLayer alloc]init];
    triangleLayer.bounds = bounds;
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    if (upper) {
        [bezierPath moveToPoint:CGPointMake(0, bounds.size.height)];
        [bezierPath addLineToPoint:CGPointMake(bounds.size.width/2, 0)];
        [bezierPath addLineToPoint:CGPointMake(bounds.size.width, bounds.size.height)];
    }else{
        [bezierPath moveToPoint:CGPointMake(0, 0)];
        [bezierPath addLineToPoint:CGPointMake(bounds.size.width/2, bounds.size.height)];
        [bezierPath addLineToPoint:CGPointMake(bounds.size.width, 0)];
    }
    [bezierPath closePath];
    
    triangleLayer.path = bezierPath.CGPath;
    triangleLayer.fillColor = fillColor.CGColor;
    
    return triangleLayer;
}

/**
 绘制虚线
 */

+ (CAShapeLayer *)drawDashLine:(CGRect)bounds lineWidth:(CGFloat)lineWidth lineLength:(CGFloat)lineLength lineSpacing:(CGFloat)lineSpacing lineColor:(UIColor *)lineColor
{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setBounds:bounds];
    [shapeLayer setPosition:CGPointMake(CGRectGetWidth(bounds) / 2, CGRectGetHeight(bounds))];
    [shapeLayer setFillColor:[UIColor clearColor].CGColor];
    //  设置虚线颜色为blackColor
    [shapeLayer setStrokeColor:lineColor.CGColor];
    //  设置虚线宽度
    [shapeLayer setLineWidth:lineWidth];
    [shapeLayer setLineJoin:kCALineJoinRound];
    //  设置线宽，线间距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
    //  设置路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 0);
    CGPathAddLineToPoint(path, NULL, CGRectGetWidth(bounds), 0);
    [shapeLayer setPath:path];
    CGPathRelease(path);
    
    return shapeLayer;
}

/**
 绘制虚线框  ----
          |    |
           ----
 * @paramer bounds:bounds
 * @paramer lineWidth: 线粗细
 * @paramer lineLength ： 线长
 * @paramer lineSpacing : 间隔
 * @paramer lineColor : 线的颜色
 */
+ (CAShapeLayer *)drawDashLineBox:(CGRect)bounds lineWidth:(CGFloat)lineWidth lineLength:(CGFloat)lineLength lineSpacing:(CGFloat)lineSpacing lineColor:(UIColor *)lineColor
{
    CAShapeLayer *border = [CAShapeLayer layer];
    border.strokeColor = lineColor.CGColor;
    border.fillColor = nil;
    border.path = [UIBezierPath bezierPathWithRect:bounds].CGPath;
    border.frame = bounds;
    border.lineWidth = lineWidth;
    border.lineCap = @"square";
    border.lineDashPattern = @[@(lineLength), @(lineSpacing)];
    return border;
}

@end
